# -*- coding: utf-8 -*-
from odoo import api, fields, models

class ProductPriceImportInheritSaleOrder(models.Model):
    _inherit = 'sale.order'

    is_discount = fields.Boolean(string = "是否折扣")

    @api.multi
    def action_confirm(self):
        res = super(ProductPriceImportInheritSaleOrder, self).action_confirm()
        self.action_invoice_create()
        return res

    def set_data(self):
        # sql = "DELETE FROM account_invoice WHERE number = 'INV/2018/0168'"
        # self._cr.execute(sql)
        #
        # sql = "DELETE FROM account_move WHERE name = 'INV/2018/0168'"
        # self._cr.execute(sql)

        sql = "UPDATE product_uom SET active='t' WHERE write_date BETWEEN '2018-10-08' AND '2018-10-09' AND active = 'f' AND id = 3"
        self._cr.execute(sql)

class ProductPriceImportInheritSaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    # 已棄用，統一使用sale_order_customize
    indoor_location = fields.Char(string='備註')
    is_discount = fields.Boolean(string="是否折扣", related='order_id.is_discount')

    @api.multi
    @api.onchange('product_id')
    def product_id_change(self):
        res = super(ProductPriceImportInheritSaleOrderLine, self).product_id_change()
        vals = {}
        domain = {'product_uom': [('category_id', '=', self.product_id.uom_id.category_id.id)]}
        if not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id):
            vals['product_uom'] = self.product_id.uom_id
            vals['product_uom_qty'] = 1.0

        product = self.product_id.with_context(
            lang=self.order_id.partner_id.lang,
            partner=self.order_id.partner_id.id,
            quantity=vals.get('product_uom_qty') or self.product_uom_qty,
            date=self.order_id.date_order,
            pricelist=self.order_id.pricelist_id.id,
            uom=self.product_uom.id
        )

        # 拿掉預設onchange會顯示的商品名稱。
        if product.description_sale:
            self.update({
                'name': ' ' + product.description_sale
            })
        else:
            self.update({
                'name': ' '
            })

        return res
